package com.modelscope.ai.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modelscope.ai.domain.dto.UserDto;
import com.modelscope.ai.domain.dto.UserQueryCriteria;
import com.modelscope.ai.domain.entity.R;
import com.modelscope.ai.domain.vo.UserVo;
import com.modelscope.ai.service.UserService;
import com.modelscope.ai.utils.RequestHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.ws.rs.PUT;

// @Tag	用在controller类上，描述此controller的信息
//@Operation	用在controller的方法里，描述此api的信息
//@Parameter	用在controller方法里的参数上，描述参数信息
//@Parameters	用在controller方法里的参数上
//@Schema	用于Entity，以及Entity的属性上
//@ApiResponse	用在controller方法的返回值上
//@ApiResponses	用在controller方法的返回值上
//@Hidden	用在各种地方，用于隐藏其api
//————————————————
@RequiredArgsConstructor
@Slf4j
@Validated
@RestController
@RequestMapping("/user")
@Tag(name = "user", description = "用户相关接口")
public class UserController {

    private final UserService userService;

    /**
     * 根据条件查询用户信息
     *
     * @param criteria
     * @return
     */
    @Operation(summary = "批量获取用户信息", description = "批量获取用户信息，用于页面展示")
    @PostMapping("/list")
    public R<Page<UserVo>> queryAll(@Valid @RequestBody UserQueryCriteria criteria) {
        Long uid = RequestHolder.get().getUid();
        log.info("批量获取用户信息-->uid:{},query:{}", uid, criteria);
        return R.ok(userService.queryAll(criteria));
    }

    @Operation(summary = "根据id获取用户信息", description = "查询用户信息，用于个人页面展示")
    @GetMapping
    public R<UserVo> queryById() {
        Long uid = RequestHolder.get().getUid();
        log.info("UserController-->uid:{}", uid);
        return R.ok(userService.queryById(uid));
    }

    @PostMapping
    public R<Page<UserVo>> add(@RequestBody UserQueryCriteria criteria) {
        return R.ok(userService.queryAll(criteria));
    }

    @PutMapping
    public R<String> update(@RequestBody UserDto criteria) {
        Long uid = RequestHolder.get().getUid();
        criteria.setId(uid);
        userService.updateUser(criteria);
        return R.ok("修改用户成功");
    }

    // 删除用户信息
    @DeleteMapping
    public R<String> delete(Long uid) {
//        Long uid = RequestHolder.get().getUid();

        userService.deleteUser(uid);
        return R.ok(null);
    }

}
